/**
 * Notes: 用户管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux@qq.com
 * Date: 2022-01-22 10:20:00 
 */

const BaseAdminService = require('./base_admin_service.js');
const UserModel = require('../../model/user_model.js');
const util = require('../../../framework/utils/util.js');
const timeUtil = require('../../../framework/utils/time_util.js');

class AdminUserService extends BaseAdminService {

    /** 获得某个用户信息 */
    async getUser({
        userId,
        fields = '*'
    }) {
        let where = {
            USER_MINI_OPENID: userId,
        }
        return await UserModel.getOne(where, fields);
    }

    /** 取得用户分页列表 */
    async getUserList({
        search, // 搜索条件
        sortType, // 搜索菜单
        sortVal, // 搜索菜单
        orderBy, // 排序
        whereEx, //附加查询条件
        page,
        size,
        oldTotal = 0
    }) {

        orderBy = orderBy || {
            USER_ADD_TIME: 'desc'
        };
        let fields = '*';

        let where = {};
        if (util.isDefined(search) && search) {
            where.USER_NAME = {
                $regex: '.*' + search,
                $options: 'i'
            };
        } else if (sortType && util.isDefined(sortVal)) {
            switch (sortType) {
                case 'status':
                    where.USER_STATUS = Number(sortVal);
                    break;
                case 'sort':
                    if (sortVal == 'new') {
                        orderBy = {
                            'USER_ADD_TIME': 'desc'
                        };
                    }
                    break;
            }
        }
        let result = await UserModel.getList(where, fields, orderBy, page, size, true, oldTotal);

        return result;
    }

    async statusUser(id, status) {
        let data = {
            USER_STATUS: status
        }
        await UserModel.edit(id, data);
    }

    async delUser(id) {
        let where = {
            _id: id
        }
        await UserModel.del(where);
    }
}

module.exports = AdminUserService;